package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.ReturnData;
import com.example.demo.entity.Stu;
import com.example.demo.mapper.StuMapper;
import com.example.demo.service.StuServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @Author lands
 * @Date 2024/11/5 8:11
 * @Description TODO
 */
@Service
public class StuServerImp implements StuServer {
    @Autowired
    StuMapper stuMapper;//引入数据库操作类
    @Override
    public ReturnData getList(String name, int age, String no) {

        QueryWrapper queryWrapper =  new QueryWrapper<>();
        // queryWrapper.ne("name","王五");
        // queryWrapper.ge("age",60);
        // queryWrapper.le("age",10);
        // 10 到20
        // queryWrapper.between("age",10,20);
        queryWrapper.like("name",name);
        queryWrapper.like("no",no);
        if(age!=0){
            queryWrapper.eq("age",age);
        }
        // String[] arr = {"12","11"};
        // queryWrapper.in("age", arr);
        // queryWrapper.groupBy("age");
        return ReturnData.ok(stuMapper.selectList(queryWrapper));
    }

    @Override
    public ReturnData deleteStu(int id) {
        return ReturnData.ok(stuMapper.deleteById(id));
    }

    @Override
    public int deleteStuXml(int id) {
        int res = stuMapper.delete(id);
        return  res;
    }

    @Override
    public ReturnData add(String name, int age, String no) {
        Stu stu = new Stu();
        stu.setAge(age);
        stu.setName(name);
        stu.setNo(no);
        int res = stuMapper.insert(stu);
        return ReturnData.ok(res);
    }

    @Override
    public int addStuXml(String name, int age, String no) {
        int res = stuMapper.add(name,no,age);
        return res;
    }

    @Override
    public int updatePlus(Stu stu) {
        int res =  stuMapper.updateById(stu);
        return res;
    }

    @Override
    public int updateXml(String name, int age, String no, int id) {
        int res = stuMapper.updateXml(name,no,age,id);
        return res;
    }

    @Override
    public List<Stu> getAllList() {
        return stuMapper.getAllList();
    }

    @Override
    public Stu selectByName(String name) {
        return stuMapper.selectByName(name);
    }

    @Override
    public List<Stu> getStuByAge(int age) {
        QueryWrapper<Stu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("age",age);

        return stuMapper.selectList(queryWrapper);
    }

    @Override
    public ReturnData uploadAvatar(int id, MultipartFile file) {
        String filename  = file.getOriginalFilename();//R-C.jpg
        String[] arr = filename.split("\\.");
        filename = UUID.randomUUID().toString()+"."+ arr[arr.length-1];

        try {
            file.transferTo(new File("D:\\avatar", filename));
        }catch (Exception e){
            e.printStackTrace();
            return ReturnData.error("上传失败");
        }
        Stu stu = stuMapper.selectById(id);//根据ID查询学生
        stu.setAvatar("http://localhost:8082/avatar/"+filename);//更新学生的头像
        stuMapper.updateById(stu);//吧更新后的数据更新回数据库
        return ReturnData.ok("http://localhost:8082/avatar/"+filename);
    }
}
